/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.MarkedJson;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.MarkedJsonRepository;
import me.zhengjie.modules.system.service.MarkedJsonService;
import me.zhengjie.modules.system.service.dto.MarkedJsonDto;
import me.zhengjie.modules.system.service.dto.MarkedJsonQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.MarkedJsonMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author fzuir
* @date 2021-01-20
**/
@Service
@RequiredArgsConstructor
public class MarkedJsonServiceImpl implements MarkedJsonService {

    private final MarkedJsonRepository markedJsonRepository;
    private final MarkedJsonMapper markedJsonMapper;

    @Override
    public Map<String,Object> queryAll(MarkedJsonQueryCriteria criteria, Pageable pageable){
        Page<MarkedJson> page = markedJsonRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(markedJsonMapper::toDto));
    }

    @Override
    public List<MarkedJsonDto> queryAll(MarkedJsonQueryCriteria criteria){
        return markedJsonMapper.toDto(markedJsonRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public MarkedJsonDto findById(Long markedJsonId) {
        MarkedJson markedJson = markedJsonRepository.findById(markedJsonId).orElseGet(MarkedJson::new);
        ValidationUtil.isNull(markedJson.getMarkedJsonId(),"MarkedJson","markedJsonId",markedJsonId);
        return markedJsonMapper.toDto(markedJson);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MarkedJsonDto create(MarkedJson resources) {
        return markedJsonMapper.toDto(markedJsonRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MarkedJson resources) {
        MarkedJson markedJson = markedJsonRepository.findById(resources.getMarkedJsonId()).orElseGet(MarkedJson::new);
        ValidationUtil.isNull( markedJson.getMarkedJsonId(),"MarkedJson","id",resources.getMarkedJsonId());
        markedJson.copy(resources);
        markedJsonRepository.save(markedJson);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long markedJsonId : ids) {
            markedJsonRepository.deleteById(markedJsonId);
        }
    }

    @Override
    public void download(List<MarkedJsonDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MarkedJsonDto markedJson : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("保存路径", markedJson.getFilePath());
            map.put("文件名", markedJson.getFileName());
            map.put("有多少框", markedJson.getNumBox());
            map.put("0未识别/1已识别", markedJson.getStatus());
            map.put("识别结果json路径", markedJson.getResultPath());
            map.put("标注任务的id", markedJson.getTaskMarkId());
            map.put("标注人员的id", markedJson.getUserId());
            map.put("图片id", markedJson.getImageId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}